#'###########################################
#'                                          #
#'  ------------------------------------    #
#'  |     COIN     __                  |    #
#'  |            >(' )                 |    #
#'  |              )/                  |    #
#'  |             /  `----/            |    #
#'  |             \  ~=- /             |    #
#'  |   ~^~^~^~^~^~^~^~^~^~^~^~^~^~^   |    #
#'  ------------------------------------    #
#'                                          #
#'###########################################


TYPE_STATIC <- "F"
TYPE_MOBILE <- "M"


INFO_ID_COLLIER <- "ID_collier"
INFO_TYPE <- "Type"
INFO_VISIBILITE <- "Visibilite"
INFO_DATE_DEBUT <- "Date_debut"
INFO_DATE_FIN <- "Date_fin"
INFO_DATE <- "Date"
INFO_GPS <- "GPS"

#'####
#'#  Fonction permettant de creer l'arborescence des donnees a partir des donnees brutes
#'#  et du fichier d'information par session. Ceci permet de separer les donnees par jour,
#'#  par type de test et par visibilite pour les tests fixes.
#'#  
#'#  Arguments:
#'#  info : informations sur les sessions
#'#  data : donnees brutes
#'#  path : chemin ou creer l'arborescence
#'#  staticPath : nom du repertoire contenant les tests statiques
#'#  mobilePath : nom du repertoire contenant les tests mobiles
#'###
createTree <- function(info, data, destpath=".", staticPath="Statique", mobilePath="Mobile") {
	for (i in 1:nrow(info)) {
		collar <- info[i, INFO_ID_COLLIER]
		type <- info[i, INFO_TYPE]
		
		setkey(data, Platform.ID.)
		collarData <- data[J(collar), ]
		
		if (type == TYPE_STATIC) {
			visibility <- paste("V", info[i, INFO_VISIBILITE], sep="")
			dates <- paste(format(info[i, INFO_DATE_DEBUT], format="%d"), "-", 
					format(info[i, INFO_DATE_FIN], format="%d%m%y"), sep="")
			
			path <- file.path(destpath, staticPath, visibility, dates)
			
			if (!file.exists(path)) {
				dir.create(path, showWarnings=FALSE, recursive=TRUE)
			}
			
			setkey(collarData, Jour)
			collarData <- collarData[Jour >= info[i, INFO_DATE_DEBUT] & Jour <= info[i, INFO_DATE_FIN]]
			
			collarData$Jour <- as.factor(collarData$Jour)
			setkey(collarData, Jour)
			for (j in levels(collarData$Jour)){
				tmppath <- paste(path, "/", format(strptime(j, format="%Y-%m-%d"), format="%d%m%y"), 
						sep="")
				if (!file.exists(tmppath)) {
					dir.create(tmppath, showWarnings=FALSE, recursive=TRUE)
				}
				write.table(collarData[J(j),], paste(tmppath, "/", collar, ".txt",sep=""))
			}
			
			
		} else if (type == TYPE_MOBILE) {
			date <- format(info[i, INFO_DATE], format="%d%m%y")
			gps <- paste("GPS", info[i, INFO_GPS], sep="")
			
			path <- file.path(destpath, mobilePath, date, gps)
			
			if (!file.exists(path)) {
				dir.create(path, showWarnings=FALSE, recursive=TRUE)
			}
			setkey(collarData, Jour)
			collarData <- collarData[J(info[i, INFO_DATE]),]
			
			write.table(collarData, paste(path, "/", collar, ".txt",sep=""))
		}
	}
}


parseSessionInfo <- function(path, dateFormat, sep=",") {
	sessionInfo <- read.delim(path, sep=sep)
	sessionInfo$Date_debut <- as.POSIXct(sessionInfo$Date_debut, format=dateFormat)
	sessionInfo$Date_fin <- as.POSIXct(sessionInfo$Date_fin, format=dateFormat)
	sessionInfo$Date <- as.POSIXct(sessionInfo$Date, format=dateFormat)
	return (sessionInfo)
}

parseData <- function(path, dateFormat) {
	data <- data.table(read.table(path, header=TRUE))
	data$Jour <- as.POSIXct(data$Jour, format="%Y/%m/%d")
	setkey(data, Platform.ID.)
	return (data)
}

